package nlp;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class Patterns {
	
	/* POS tags given by the classifier
	noun
	verb
	adjective
	pronoun
	determiner
	adverb
	proposition
	conjunction
	numeral
	interjection
	punctuation
	symbol
	electronic
	
	Our universal POS tagset uniﬁes this previous work and deﬁnes the following twelve POS tags: 
	NOUN (nouns), 
	VERB (verbs), 
	ADJ (adjectives), 
	ADV (adverbs), 
	PRON (pronouns), 
	DET (determiners and articles), 
	ADP (prepositions and postpositions), 
	NUM (numerals), 
	CONJ (conjunctions), 
	PRT (particles), 
	‘.’ (punctuation marks) 
	and X (a catch-all for other categories such as abbreviations or foreign words).
	
	*/
	
	//TODO: HearstPatterns
	public static void extractHearst(String[] sourceTokens, String[] sourcePOS) {
		
	}
	
	// ? one or nothing
	// * zero or more
	// + one or more
	
	//TODO: noun-phrases
	// (Adj|Noun)+|
	// (Adj|Noun)*(NounPrep)?)(Adj|Noun)*)Noun
	
	/*
	NP([n])
	NP([art,NP])
	NP([NP,num])
	NP([num,NP])
	• NP([art,“só”,NP])
	NP([NP,prep,num])
	• NP([art,“cerca de”,NP])
	NP([NP,prep,art,num])
	NP([adv,NP])
	NP([NP,prep,art,adj])
	NP([nprop])
	NP([NP,adj])
	NP([NP,pcp])
	NP([proadj,NP])
	NP([NP,prep,adj])
	NP([art,adv,NP])
	NP([NP,kc,adj])
	NP([NP,prep,prep,NP])
	NP([art,adj,kc,NP])
	NP([NP,kc,adv,adj])
	NP([adj,NP])
	NP([NP,adv])
	NP([NP,proadj])
	• NP([art,“então”,NP])
	NP([NP,prep,NP])
	NP([NP,prep,adv])
	NP([NP,kc,num])
	NP([proadj,adv,NP])
	NP([NP,pden,NP])
	NP([NP,prep,proadj,kc,NP])
	*/
	
	public static List<ArrayList<Integer>> extractNounPhrases(String[] sourceTokens, String[] sourcePOS){
		String noun_phrase = null;
		ArrayList<String> nps = new ArrayList<String>();
		for ( int i = 0 ; i < sourceTokens.length; i++ ) {		
			if ( sourcePOS[i].startsWith("noun") || sourcePOS[i].startsWith("adjective") ) {
				
			}			
		}
		return null;
	}
	
	/*  
	V = verb particle? adv?
	W = (noun j adj j adv j pron j det)
	P = (prep j particle j inf. marker)

	TODO: ter em consideração a negação:
			sourceTokens[i-1].equals("não") ||
			sourceTokens[i-1].equals("nunca") ||
			sourceTokens[i-1].equals("jamais") ||
			sourceTokens[i-1].equals("ninguém") ||
			sourceTokens[i-1].equals("nada") ||
	 */
	
	public static List<ArrayList<Integer>> extractReVerbPatterns(String[] sourceTokens, String[] sourcePOS) {				
		List<ArrayList<Integer>> patterns = new LinkedList<ArrayList<Integer>>();		
		for ( int i = 0 ; i < sourceTokens.length; i++ ) {		
			if ( sourcePOS[i].startsWith("verb") || sourcePOS[i].startsWith("pp") ) { //verb pode estar também no partícpio passado
				String pattern = null;
				ArrayList<Integer> tokens = new ArrayList<Integer>();
				tokens.add(i);				
				pattern = sourceTokens[i].toLowerCase();
				
				//caso em que o verbo é um verbo auxilar: ser, estar, ter, ir, haver, etc.
				//se o próximo token for um verbo também, ver se a sequência de tokens é um padrão ReVerb
				
				if (sourcePOS[i+1].startsWith("verb") || sourcePOS[i+1].startsWith("pp")) {										
					pattern += " " + sourceTokens[i+1].toLowerCase();
					tokens.add(i+1);
					int j = i;
					//ReVerb inspired: um verbo, seguido de vários nomes, adjectivos ou adverbios, terminando numa preposição.					
					if (i+2 < sourceTokens.length - 2) {	  			
						j = j+2;
						while ( ((j < sourceTokens.length - 2)) && ( 
								sourcePOS[j].startsWith("adverb") || 
								sourcePOS[j].startsWith("adjective") || 
								sourcePOS[j].startsWith("noun") ||
								sourcePOS[j].startsWith("pronoun") ))  {
						pattern += " " + sourceTokens[j].toLowerCase();
						tokens.add(j);
						j++;
						}
					}
					i=j;
				}				
				else {
					//ReVerb inspired: um verbo, seguido de vários nomes, adjectivos ou adverbios, terminando numa preposição.					
					if (i < sourceTokens.length - 2) {	  			
						int j = i+1;
						while ( ((j < sourceTokens.length - 2)) && ( 
								sourcePOS[j].startsWith("adverb") || 
								sourcePOS[j].startsWith("adjective") || 
								sourcePOS[j].startsWith("noun") ||
								sourcePOS[j].startsWith("pronoun") ))  {
						pattern += " " + sourceTokens[j].toLowerCase();
						tokens.add(j);
						j++;					
						}
						i=j;
					}
				}
				if ( sourcePOS[i].startsWith("preposition") || sourcePOS[i].startsWith("determiner")) {
					pattern += " " + sourceTokens[i].toLowerCase();
					tokens.add(i);
				}				
				//System.out.println("pattern: " + pattern);
				patterns.add(tokens);			
			}
		}
		return patterns;
	}
}